package com.example.simpledemo1.aggregate.sortDemo;

import java.util.Set;
import java.util.TreeMap;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: ZhangLiang
 * @Date: 2021/06/29/16:08
 * @Description: 重写compareTo实现排序
 */
public class ComparableDemo  implements Comparable<ComparableDemo>{
    private long id;
    private String name;

    public ComparableDemo(long id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    @Override
    public int compareTo(ComparableDemo o) {
       if (this.id>o.getId()){
           return -1;
       }
        if (this.id<o.getId()){
            return 1;
        }
        return 0;
    }

    public static void main(String[] args) {
        TreeMap<ComparableDemo,String> data=new TreeMap<>();
        data.put(new ComparableDemo(1,"张三"),"zhangsan");
        data.put(new ComparableDemo(2,"李四"),"lisi");
        data.put(new ComparableDemo(3,"王五"),"wangwu");
        data.put(new ComparableDemo(4,"赵六"),"zhaoliu");
        // 得到key的值的同时得到key所对应key的值
        Set<ComparableDemo> keys=data.keySet();
        for (ComparableDemo key:keys
             ) {
            System.out.println(key.getId()+"-"+key.getName());
        }
    }
}
